package main

import (
	"fmt"
	jsoniter "github.com/json-iterator/go"
)

type User struct {
	Id      int    `json:"id"`
	Name    string `json:"username"`
	Age     int    `json:"age,omitempty"` // omitempty 会忽略缺省值
	Address string `json:"-"`             // - 一定不会被序列化 绝对忽略
}

func main() {
	// fast,
	var json = jsoniter.ConfigCompatibleWithStandardLibrary
	var u = User{
		Id:      1001,
		Name:    "jack",
		Age:     1,
		Address: "上海",
	}
	// 序列化，转化为json格式
	data, err := json.Marshal(&u)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(data))
	//data := []byte(`{"id":1001,"username":"jack","age":1}`)
	// 反序列化，json字符串 --> 对象
	//var obj User
	obj := &User{}
	err = json.Unmarshal(data, &obj)
	if err != nil {
		fmt.Println(err)
	}
	//fmt.Printf("%+v \n", u)
	fmt.Printf("%+v \n", obj)
}
